package com.ncloud.resman.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.constant.ResConstant;
import com.ncloud.resman.entity.CpeModel;
import com.ncloud.resman.entity.FirewallModel;
import com.ncloud.resman.entity.Model;
import com.ncloud.resman.entity.ServerModel;
import com.ncloud.resman.entity.SwitchModel;
import com.ncloud.resman.service.ModelService;
import com.ncloud.resman.service.transform.ModelTransService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Created by gaowei on 2017/7/27.
 */
@Api(value = "自动化运维设备型号管理", description = "设备型号管理", tags = "设备型号管理",
basePath="/",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/resman")
public class ModelController {
	@Autowired
	private ModelService modelService;
	@Autowired
	private ModelTransService transService;
	
	@ApiOperation(value="list models", notes="获取设备型号列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="query", name = "eqptType", value = "设备类型", required = false, dataType = "String" ),
        @ApiImplicitParam(paramType="query", name = "brandId", value = "品牌", required = false, dataType = "String" ),
        @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页数量", required = false, dataType = "Long" ),
        @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = false, dataType = "Long" )
	})
    @GetMapping(value = "/models")
    public Result<?> getModelList(@RequestParam(required=false) String eqptType, @RequestParam(required=false) Long brandId, 
    		@RequestParam(required=false) Long pageSize, @RequestParam(required=false) Long pageNum,HttpServletRequest request){
		JSONObject json = new JSONObject();
		if(StringUtils.isNotBlank(eqptType)){
			json.put("eqptType", eqptType);
		}
		if(brandId !=null){
			json.put("brandId", brandId);
		}
		if(pageSize != null && pageNum != null){
			json.put("pageSize", pageSize);
			json.put("pageNum", pageNum);
		}
    	Object brandList = modelService.getModelList(json);
		return ResultUtil.success(brandList);
    }
	
	@ApiOperation(value="model", notes="获取设备型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(paramType="path",name = "modelId", value = "设备型号编码", required = true, dataType = "Long" )
	})
    @GetMapping(value = "/model/{modelId}")
    public Result<?> getModelInfo(@PathVariable Long modelId,HttpServletRequest request){
		JSONObject model = modelService.getModelById(modelId);
		return ResultUtil.success(model);
    }
	
	@ApiOperation(value="add model server", notes="创建服务器型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "model", value = "服务器型号信息", required = true, dataType = "ServerModel" )
	})
    @PostMapping(value = "/model/server")
    public Result<?> addServer(HttpServletRequest request, @RequestBody ServerModel model){
    	Object userID = request.getAttribute("userID");
    	Long userId = Long.parseLong(userID.toString());
    	model.setEqptType(ResConstant.EQPT_TYPE_SERVER);
    	model.setCreateUser(userId);
    	model.setCreateDate(new Date());
    	model.setUpdateDate(new Date());
    	model.setUpdateUser(userId);
    	modelService.insertServerModel(model);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="add model switch", notes="创建交换机型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "model", value = "交换机型号信息", required = true, dataType = "SwitchModel" )
	})
    @PostMapping(value = "/model/switch")
    public Result<?> addSwitch(HttpServletRequest request, @RequestBody SwitchModel model){
    	Object userID = request.getAttribute("userID");
    	Long userId = Long.parseLong(userID.toString());
    	model.setEqptType(ResConstant.EQPT_TYPE_SWITCH);
    	model.setCreateUser(userId);
    	model.setCreateDate(new Date());
    	model.setUpdateDate(new Date());
    	model.setUpdateUser(userId);
    	modelService.insertSwitchModel(model);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="add model cpe", notes="创建cpe型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "model", value = "cpe型号信息", required = true, dataType = "CpeModel" )
	})
    @PostMapping(value = "/model/cpe")
    public Result<?> addCpe(HttpServletRequest request, @RequestBody CpeModel model){
    	Object userID = request.getAttribute("userID");
    	Long userId = Long.parseLong(userID.toString());
    	model.setEqptType(ResConstant.EQPT_TYPE_CPE);
    	model.setCreateUser(userId);
    	model.setCreateDate(new Date());
    	model.setUpdateDate(new Date());
    	model.setUpdateUser(userId);
    	modelService.insertCpeModel(model);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="add model firewall", notes="创建防火墙型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "model", value = "防火墙型号信息", required = true, dataType = "FirewallModel" )
	})
    @PostMapping(value = "/model/firewall")
    public Result<?> addFirewall(HttpServletRequest request, @RequestBody FirewallModel model){
    	Object userID = request.getAttribute("userID");
    	Long userId = Long.parseLong(userID.toString());
    	model.setEqptType(ResConstant.EQPT_TYPE_FIREWALL);
    	model.setCreateUser(userId);
    	model.setCreateDate(new Date());
    	model.setUpdateDate(new Date());
    	model.setUpdateUser(userId);
    	modelService.insertModel(model);
		return ResultUtil.success();
    }
    
	@ApiOperation(value="update server model", notes="更新服务器设备型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "serverModel", value = "服务器设备型号信息", required = true, dataType = "ServerModel" )
	})
    @PutMapping(value = "/model/server")
    public Result<?> updateServerModel(HttpServletRequest request, @RequestBody ServerModel serverModel){
    	Object userID = request.getAttribute("userID");
    	serverModel.setEqptType(ResConstant.EQPT_TYPE_SERVER);
    	serverModel.setUpdateUser(Long.parseLong(userID.toString()));
    	serverModel.setUpdateDate(new Date());
    	modelService.updateServerModel(serverModel);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="update switch model", notes="更新交换设备型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "switchModel", value = "交换机设备信息", required = true, dataType = "SwitchModel" )
	})
    @PutMapping(value = "/model/switch")
    public Result<?> updateSwitchModel(HttpServletRequest request, @RequestBody SwitchModel switchModel){
    	Object userID = request.getAttribute("userID");
    	switchModel.setEqptType(ResConstant.EQPT_TYPE_SWITCH);
    	switchModel.setUpdateUser(Long.parseLong(userID.toString()));
    	switchModel.setUpdateDate(new Date());
    	modelService.updateSwitchModel(switchModel);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="update cpe model", notes="更新CPE设备型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "cpeModel", value = "CPE设备信息", required = true, dataType = "CpeModel" )
	})
    @PutMapping(value = "/model/cpe")
    public Result<?> updateCpeModel(HttpServletRequest request, @RequestBody CpeModel cpeModel){
    	Object userID = request.getAttribute("userID");
    	cpeModel.setEqptType(ResConstant.EQPT_TYPE_CPE);
    	cpeModel.setUpdateUser(Long.parseLong(userID.toString()));
    	cpeModel.setUpdateDate(new Date());
    	modelService.updateCpeModel(cpeModel);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="update fireWall model", notes="更新防火墙设备型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "firewallModel", value = "防火墙设备信息", required = true, dataType = "FirewallModel" )
	})
    @PutMapping(value = "/model/fireWall")
    public Result<?> updateFireWallModel(HttpServletRequest request, @RequestBody FirewallModel firewallModel){
    	Object userID = request.getAttribute("userID");
    	firewallModel.setEqptType(ResConstant.EQPT_TYPE_FIREWALL);
    	firewallModel.setUpdateUser(Long.parseLong(userID.toString()));
    	firewallModel.setUpdateDate(new Date());
    	modelService.updateFireWallModel(firewallModel);
		return ResultUtil.success();
    }
    
	@ApiOperation(value="delete model", notes="删除设备型号信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(paramType="path", name = "modelId", value = "设备型号编码", required = true, dataType = "Long" )
	})
    @DeleteMapping(value = "/model/{modelId}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("modelId") Long modelId){
		Model model = new Model();
		model.setModelId(modelId);
		Object userID = request.getAttribute("userID");
		model.setUpdateUser(Long.parseLong(userID.toString()));
		model.setUpdateDate(new Date());
		modelService.deleteModel(model);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="import model list", notes="导入设备型号列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="form",name = "eqptType", value = "设备类型", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="form",name = "file", value = "excel附件", required = true, dataType = "MultipartFile" )
	})
    @PostMapping(value = "/models/import")
	public Result<?> importModel(HttpServletRequest request,@RequestParam String eqptType,
			@RequestParam MultipartFile file) throws IOException {
		Object userID = request.getAttribute("userID");
		Long userId = Long.parseLong(userID.toString());
		transService.importModel(eqptType, userId, file.getInputStream());
		return ResultUtil.success();
	}
	
	@ApiOperation(value="export brand list", notes="导入品牌列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="query",name = "eqptType", value = "设备类型", required = false, dataType = "String" ),
		@ApiImplicitParam(paramType="query",name = "brandId", value = "品牌编码", required = false, dataType = "Long" )
	})
    @GetMapping(value = "/models/export")
	public Result<?> exportModel(@RequestParam(value = "eqptType", required = true) String eqptType,
			@RequestParam(value = "brandId", required = false) Long brandId, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		transService.exportModel(eqptType,brandId, response,false);
		return ResultUtil.success();
	}
	
	@ApiOperation(value="import brand list", notes="导入品牌列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="query",name = "eqptType", value = "设备类型", required = false, dataType = "String" ),
		@ApiImplicitParam(paramType="query",name = "brandId", value = "品牌编码", required = false, dataType = "Long" )
	})
    @GetMapping(value = "/models/exportTemplate")
	public Result<?> exportModelTemplate(@RequestParam(value = "eqptType", required = true) String eqptType,
			HttpServletRequest request,	HttpServletResponse response) throws IOException {
		transService.exportModel(eqptType,null, response,true);
		return ResultUtil.success();
	}
    
}
